﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Automobile.service.BLL;
using Automobile.service.Model;
using System.Data;

namespace Automobile.service.UI
{
    public partial class Stock_Edit : System.Web.UI.Page
    {
        StockBLL bll = new StockBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                StockLoad();
                DropDownList1DataBind();
                DropDownList2DataBind();
            }
        }
        protected void StockLoad()
        {
            int id = Convert.ToInt32(Request.QueryString["sid"]);
            DataTable table = bll.GetStockByid(id);
            this.HiddenField1.Value = id.ToString();
            DropDownList1.SelectedValue = table.Rows[0]["WareHose_Id"].ToString();
            DropDownList2.SelectedValue = table.Rows[0]["Product_Id"].ToString();
            Stock_Num.Value = table.Rows[0]["Stock_Num"].ToString();
            Stock_Warn.Value = table.Rows[0]["Stock_Warn"].ToString();  
        }
        protected void DropDownList1DataBind()
        {
            WareHoseBLL bll = new WareHoseBLL();
            DropDownList1.DataSource = bll.ShowListWareHose();
            DropDownList1.DataTextField = "WareHose_Name";
            DropDownList1.DataValueField = "WareHose_Id";
            DropDownList1.DataBind();
        }
        protected void DropDownList2DataBind()
        {
            ProductBLL bll = new ProductBLL();
            DropDownList2.DataSource = bll.ShowProduc();
            DropDownList2.DataTextField = "Product_Name";
            DropDownList2.DataValueField = "Product_Id";
            DropDownList2.DataBind();
        }
        protected void Button1_Click(object souce,EventArgs e)
        {
            Stock s = new Stock();
            s.Stock_Id = Convert.ToInt32(this.HiddenField1.Value);
            s.Stock_num = Convert.ToInt32(Stock_Num.Value);
            s.Stock_warn = Convert.ToInt32(Stock_Warn.Value);
            s.WareHose_Id = Convert.ToInt32(DropDownList1.SelectedValue);
            s.Product_Id = Convert.ToInt32(DropDownList2.SelectedValue);
            if(bll.UpdStockById(s)>0)
            {
                JournalBLL jbll = new JournalBLL();
                jbll.AddJourna_UpdStockById(Convert.ToInt32(Session["empid"]),s);
                Response.Write("<script>alert('修改成功');parent.location.reload();parent.layer.close(parent.layer.getFrameIndex(window.name));</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败');parent.layer.close(parent.layer.getFrameIndex(window.name));</script>");
            }
        }
    }
}